package com.b511.swpu.cementbackend.controller;

import com.b511.swpu.cementbackend.entity.SysUser;
import com.b511.swpu.cementbackend.service.BoreholeService;
import com.b511.swpu.cementbackend.service.MetaDictService;
import com.b511.swpu.cementbackend.dto.input.BoreholeConditionInputDto;
import com.b511.swpu.cementbackend.dto.input.MetaDictInputDto;
import com.b511.swpu.cementbackend.entity.Borehole;
import com.b511.swpu.cementbackend.repository.BoreholeRepository;
import com.b511.swpu.cementbackend.util.R;
import io.swagger.annotations.Api;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(value = "井眼概述", tags = {"井眼概述"})
@RestController
@RequestMapping("/borehole")
public class BoreholeController {

    private final BoreholeRepository boreholeRepository;

    private final BoreholeService boreholeService;

    private final MetaDictService metaDictService;

    public BoreholeController(BoreholeRepository boreholeRepository, BoreholeService boreholeService, MetaDictService metaDictService) {
        this.boreholeRepository = boreholeRepository;
        this.boreholeService = boreholeService;
        this.metaDictService = metaDictService;
    }

    @GetMapping
    public R getBorehole() {
        return boreholeService.getAllBorehole();
    }

    @GetMapping("/{num}/{size}")
    public R getBoreholeByCondition(@PathVariable int num, @PathVariable int size, @ModelAttribute BoreholeConditionInputDto boreholeConditionInputDto) {
        return boreholeService.getBoreholeByCondition(boreholeConditionInputDto, PageRequest.of(num, size));
    }

    @GetMapping("/dict/child")
    public R getDictByName(@ModelAttribute @Validated MetaDictInputDto metaDictInputDto) {
        return metaDictService.getDictByName(metaDictInputDto.getLabel());
    }

    @PutMapping("/{id}")
    public R updateBorehole(@PathVariable Long id, @RequestBody Borehole borehole) {
        System.out.println(borehole.getBeginDrillDate());
        return boreholeService.updateBorehole(id, borehole);
    }

    @DeleteMapping("/{id}")
    @Transactional
    public R deleteBorehole(@PathVariable Long id) {
        boreholeRepository.deleteById(id);
        return R.ok();
    }

    @PostMapping
    public R validateBorehole() {
        return boreholeService.validateBorehole();
    }
}
